package Hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.ArrayList;

/**
 * @author: yehw
 * @date: 2020/7/25 19:59
 * @description: HBase链接类
 */
public class HbaseClient {
    static Configuration configuration;
    static Connection connection;
    static HBaseAdmin admin;

    //获得链接
    public static void getCon() {
        configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "linux121,linux122");
        configuration.set("hbase.zookeeper.property.clientPort", "2181");
        try {
            connection = ConnectionFactory.createConnection(configuration);
            System.out.println("HBase链接成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //创建表
    public static void createTable() throws IOException {
        admin = (HBaseAdmin) connection.getAdmin();
        //创建表描述器
        HTableDescriptor friend = new HTableDescriptor(TableName.valueOf("friend"));
        //设置列族描述器
        friend.addFamily(new HColumnDescriptor("friends"));
        //执⾏创建操作
        admin.createTable(friend);
        System.out.println("friend表创建成功！！");
    }

    /**
     * @author:yehw
     * @date: 2020/9/3:21:56
     * @paramType:
     * @returnType:
     * @description: 初始化数据
     */
    public static void init() throws IOException {
        Table friends = connection.getTable(TableName.valueOf("friends"));
        Put uid1 = new Put(Bytes.toBytes("uid1"));
        //第一个参数是列簇，第二个参数是列名，第三个是列值
        uid1.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid2"), Bytes.toBytes("uid2"));
        uid1.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid3"), Bytes.toBytes("uid3"));
        uid1.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid4"), Bytes.toBytes("uid4"));
        Put uid2 = new Put(Bytes.toBytes("uid2"));
        uid1.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid1"), Bytes.toBytes("uid1"));
        uid1.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid3"), Bytes.toBytes("uid3"));
        uid1.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid4"), Bytes.toBytes("uid4"));
        ArrayList<Put> puts = new ArrayList<Put>();
        puts.add(uid1);
        puts.add(uid2);
        friends.put(puts);
        //friends.close();
    }
    /**
    * @author:yehw
    * @date: 2020/9/3:22:12
    * @paramType: 第一个参数是主键，第二个参数是要删除的好友
    * @returnType:
    * @description:
    */
    public static void deleteData(String uid,String fri) throws IOException {
        //需要获取⼀个table对象
        final Table friend = connection.getTable(TableName.valueOf("friend"));
        //准备delete对象
        final Delete delete = new Delete(Bytes.toBytes(uid));
        delete.addColumns(Bytes.toBytes("friends"), Bytes.toBytes(fri));
        //执⾏删除
        friend.delete(delete);
        System.out.println("删除数据成功！！");
    }

    public static void main(String[] args) throws IOException {
        /**获得链接*/
        getCon();
        /**创建表*/
        //createTable();
        /**初始化数据*/
        init();
        /**删除表*/
        deleteData("uid1","uid2");
    }

}

